package com.ghh.app.monsterrush.weapons.state;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.ghh.app.monsterrush.StateAction;
import com.ghh.app.monsterrush.WeaponActor;

public class InitializationState implements StateAction {
	private WeaponActor		weapon;
	private TextureRegion	textureRegion;
	private Animation		animation;
	private float			stateTime	= 0f;

	public InitializationState(WeaponActor weapon, Texture texture) {
		this.weapon = weapon;
		this.textureRegion = new TextureRegion(texture);
	}

	public InitializationState(WeaponActor weapon, TextureRegion[] frames, float duration) {
		this.weapon = weapon;
		animation = new Animation(duration, frames);
		animation.setPlayMode(Animation.NORMAL);
	}

	@Override
	public void start() {
		stateTime = 0f;
		weapon.setScale(0f, 0f);
		weapon.addAction(Actions.sequence(Actions.scaleTo(1, 1, 0.2f), new Action() {
			@Override
			public boolean act(float arg0) {
				weapon.onReady();
				return true;
			}
		}));
	}

	@Override
	public void act(float deltaTime) {
		stateTime += deltaTime;
	}

	@Override
	public void draw(SpriteBatch batch) {
		if (textureRegion != null) {
			weapon.drawFrame(batch, textureRegion);
		} else {
			weapon.drawFrame(batch, animation.getKeyFrame(stateTime));
		}
	}
}
